package com.hjy.ware.service.impl;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hjy.common.utils.R;
import com.hjy.order.vo.FareVo;
import com.hjy.order.vo.MemberAddressVo;
import com.hjy.ware.feign.MemberFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjy.common.utils.PageUtils;
import com.hjy.common.utils.Query;

import com.hjy.ware.dao.WareInfoDao;
import com.hjy.ware.entity.WareInfoEntity;
import com.hjy.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StrUtil.isNotBlank(key)) {
            wrapper.and(wareInfoEntityQueryWrapper -> {
                wareInfoEntityQueryWrapper.eq("id",key).or().like("name",key);
            });
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        fareVo.setFare(new BigDecimal(RandomUtil.randomInt(0,10)));
        R info = memberFeignService.info(addrId);
        Object data = info.get("memberReceiveAddress");
        MemberAddressVo memberAddressVo = JSONUtil.toBean(JSONUtil.toJsonStr(data), new TypeReference<MemberAddressVo>() {
        }, true);
        fareVo.setAddress(memberAddressVo);
        return fareVo;
    }

}